/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.HazardousChemicalsPurchase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.HazardousChemicalsPurchaseRepository;
import me.zhengjie.modules.biz.service.HazardousChemicalsPurchaseService;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsPurchaseDto;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsPurchaseQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.HazardousChemicalsPurchaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class HazardousChemicalsPurchaseServiceImpl implements HazardousChemicalsPurchaseService {

    private final HazardousChemicalsPurchaseRepository hazardousChemicalsPurchaseRepository;
    private final HazardousChemicalsPurchaseMapper hazardousChemicalsPurchaseMapper;

    @Override
    public Map<String,Object> queryAll(HazardousChemicalsPurchaseQueryCriteria criteria, Pageable pageable){
        Page<HazardousChemicalsPurchase> page = hazardousChemicalsPurchaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(hazardousChemicalsPurchaseMapper::toDto));
    }

    @Override
    public List<HazardousChemicalsPurchaseDto> queryAll(HazardousChemicalsPurchaseQueryCriteria criteria){
        return hazardousChemicalsPurchaseMapper.toDto(hazardousChemicalsPurchaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public HazardousChemicalsPurchaseDto findById(Integer id) {
        HazardousChemicalsPurchase hazardousChemicalsPurchase = hazardousChemicalsPurchaseRepository.findById(id).orElseGet(HazardousChemicalsPurchase::new);
        ValidationUtil.isNull(hazardousChemicalsPurchase.getId(),"HazardousChemicalsPurchase","id",id);
        return hazardousChemicalsPurchaseMapper.toDto(hazardousChemicalsPurchase);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HazardousChemicalsPurchaseDto create(HazardousChemicalsPurchase resources) {
        return hazardousChemicalsPurchaseMapper.toDto(hazardousChemicalsPurchaseRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(HazardousChemicalsPurchase resources) {
        HazardousChemicalsPurchase hazardousChemicalsPurchase = hazardousChemicalsPurchaseRepository.findById(resources.getId()).orElseGet(HazardousChemicalsPurchase::new);
        ValidationUtil.isNull( hazardousChemicalsPurchase.getId(),"HazardousChemicalsPurchase","id",resources.getId());
        hazardousChemicalsPurchase.copy(resources);
        hazardousChemicalsPurchaseRepository.save(hazardousChemicalsPurchase);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            hazardousChemicalsPurchaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<HazardousChemicalsPurchaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HazardousChemicalsPurchaseDto hazardousChemicalsPurchase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单号", hazardousChemicalsPurchase.getOrderNumber());
            map.put("危化品名称", hazardousChemicalsPurchase.getName());
            map.put("危化品数量", hazardousChemicalsPurchase.getAmount());
            map.put("申请人", hazardousChemicalsPurchase.getUserId());
            map.put("状态", hazardousChemicalsPurchase.getStatus());
            map.put("期望完成时间", hazardousChemicalsPurchase.getExpectedAt());
            map.put("审批人员", hazardousChemicalsPurchase.getAuditorId());
            map.put(" createdAt",  hazardousChemicalsPurchase.getCreatedAt());
            map.put(" updatedAt",  hazardousChemicalsPurchase.getUpdatedAt());
            map.put("是否删除", hazardousChemicalsPurchase.getIsDeleted());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}